using System;
using System.Data;
using SemiCASE.Business;

namespace SemiCASE.UserControls
{
    public partial class LoginControl : System.Web.UI.UserControl
    {
        public string LoginBasariliNavigateURL
        {
            get
            {
                return ViewState["LoginBasariliNavigateURL"].ToString();
            }
            set
            {
                ViewState["LoginBasariliNavigateURL"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            ViewState["update"] = Session["update"];
        }

        private void backButtonCheck()
        {
            if ((Session["update"] != null && ViewState["update"] != null) && Session["update"].ToString() == ViewState["update"].ToString())
            {
                Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
            }
            else
            {
                Session.Abandon();
                Response.Redirect("~/Hata/Hata.aspx");
            }
        }

        protected void btnTamam_Click(object sender, EventArgs e)
        {
            string tcno = txtTcno.Text;
            string sifre = txtsifre.Text;

            if (String.IsNullOrEmpty(tcno) || String.IsNullOrEmpty(sifre))
                JavascriptHelper.Alert(this.Page, "Enter User Name and Password!");

            DataTable dt = new LoginBS().IsUserValid(txtTcno.Text, txtsifre.Text);


            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0][0].ToString() != "0")
                {
                    Session["IsUserLogined"] = true;
                    Session["UserInfo"] = dt;
                    Response.Redirect("~/Projects.aspx");
                }
                else
                    Alert();
            }
            else
                Alert();
        }

        private void Alert()
        {
            JavascriptHelper.Alert(this.Page, "User Name or Password is not correct!");
        }

        protected void btnIptal_Click(object sender, EventArgs e)
        {
            JavascriptHelper.PopUpiKapat();
        }
    }
}
